#include <stdio.h>
#include "quad.h"
#include "symbol.h"

void printQuadType(enum quad_t type)
{
  switch(type)
    {
    case Q_GOTO: printf("Q_GOTO"); break;
    case Q_IFEQGOTO: printf("Q_IFEQGOTO"); break;
    case Q_IFNEQGOTO: printf("Q_IFNEQGOTO"); break;
    case Q_IFGTGOTO: printf("Q_IFGTGOTO"); break;
    case Q_IFGEGOTO: printf("Q_IFGEGOTO"); break;
    case Q_IFLTGOTO: printf("Q_IFLTGOTO"); break;
    case Q_IFLEGOTO: printf("Q_IFLEGOTO"); break;
    case Q_ASSIGN: printf("Q_ASSIGN"); break;
    case Q_PTASSIGN: printf("Q_PTASSIGN"); break;
    case Q_PTASSIGN_CST: printf("Q_PTASSIGN_CST"); break;
    case Q_NEGATE: printf("Q_NEGATE"); break;
    case Q_ADDI: printf("Q_ADDI"); break;
    case Q_MULI: printf("Q_MULI"); break;
    case Q_DIVI: printf("Q_DIVI"); break;
    case Q_MINI: printf("Q_MINI"); break;
    case Q_READI: printf("Q_READI"); break;
    case Q_PRINTI: printf("Q_PRINTI"); break;
    case Q_PRINTS: printf("Q_PRINTS"); break;
    case Q_CALL: printf("Q_CALL"); break;
    case Q_PARAM: printf("Q_PARAM"); break;
    case Q_FUNCEND: printf("Q_FUNCEND"); break;
    case Q_LABEL: printf("Q_LABEL"); break;
    case Q_END: printf("Q_END"); break;
    }
}

void printQuad (quad q)
{
  printQuadType(q.type);
  printf("\t");
  printf("a1:%p\ta2:%p\t",q.a1,q.a2);
  switch(q.type)
    {
    case Q_GOTO:
    case Q_IFEQGOTO:
    case Q_IFNEQGOTO:
    case Q_IFGEGOTO:
    case Q_IFGTGOTO:
    case Q_IFLEGOTO:
    case Q_IFLTGOTO:
    case Q_CALL:
      {
	/* case a3 is a label (an unsigned int) */
	printf("%li\n",(unsigned long int)q.a3);
      }
      break;
    default:
      {
	/* otherwise a3 is a ptr on a symbol */
	printf("%p\n",(symbol*)q.a3);
      }
      break;
    }
}

quad newQuad (unsigned int type,symbol* a1,symbol* a2,void* a3)
{
  quad q;
  q.type=type;
  q.a1=a1;
  q.a2=a2;
  q.a3=a3;

  /* a3 can either be an unsigned long int or a symbol* */

  /* switch(type) */
  /*   { */
  /*   case Q_GOTO: */
  /*   case Q_IFEQGOTO: */
  /*   case Q_IFNEQGOTO: */
  /*   case Q_IFGEGOTO: */
  /*   case Q_IFGTGOTO: */
  /*   case Q_IFLEGOTO: */
  /*   case Q_IFLTGOTO: */
  /*   case Q_CALL: /\* function call has int for number of param *\/ */
  /*     { */
  /* 	/\* a3 IS A UNSIGNED LONG INT *\/ */
  /* 	q.a3=(unsigned long int)a3; */
  /*     } */
  /*     break; */
  /*   default: */
  /*     { */
  /* 	/\* otherwise a3 is a ptr on a symbol *\/ */
  /* 	q.a3=(symbol*)a3; */
  /*     } */
  /*     break; */
  /*   } */
  return q;
}
